class Libxml2 < Formula
  desc "GNOME XML library"
  homepage "http://xmlsoft.org/"
  url "https://download.gnome.org/sources/libxml2/2.15/libxml2-2.15.1.tar.xz"
  sha256 "c008bac08fd5c7b4a87f7b8a71f283fa581d80d80ff8d2efd3b26224c39bc54c"
  license "MIT"
  revision 1

  # We use a common regex because libxml2 doesn't use GNOME's "even-numbered
  # minor is stable" version scheme.
  livecheck do
    url :stable
    regex(/libxml2[._-]v?(\d+(?:\.\d+)+)\.t/i)
  end

  bottle do
    sha256 cellar: :any,                 arm64_tahoe:   "e76405cf2d17023bd38266cc74f7ad310e29fd39828dcc2786752cb598e84d74"
    sha256 cellar: :any,                 arm64_sequoia: "b5c41a7a40c8e68e45b551d0b520adc23a9ebd4a0f3a40662e08e45dfdea0b68"
    sha256 cellar: :any,                 arm64_sonoma:  "4aa659b9756ace59200bd7ffa3f53fde9b7317ed276d5e1d63386fb12c133b4e"
    sha256 cellar: :any,                 sonoma:        "cd4e6a57e38d34a95165af7124fce4a677afe534487174e62d8a721495047415"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "0568bf7925c997532e450bc025663be693de3dda965b2f2a5aed1b067c6d18c9"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "d3d6e52d3b7b6343c519784cb4509b5082403e0d81e6e4648bcf99a47b0c75ff"
  end

  head do
    url "https://gitlab.gnome.org/GNOME/libxml2.git", branch: "master"

    depends_on "autoconf" => :build
    depends_on "automake" => :build
    depends_on "libtool" => :build
  end

  keg_only :provided_by_macos

  depends_on "pkgconf" => [:build, :test]
  depends_on "icu4c@78"
  depends_on "readline"

  uses_from_macos "zlib"

  def icu4c
    deps.find { |dep| dep.name.match?(/^icu4c(@\d+)?$/) }
        .to_formula
  end

  def install
    system "autoreconf", "--force", "--install", "--verbose" if build.head?
    system "./configure", "--disable-silent-rules",
                          "--sysconfdir=#{etc}",
                          "--with-history",
                          "--with-http",
                          "--with-icu",
                          "--with-legacy", # https://gitlab.gnome.org/GNOME/libxml2/-/issues/751#note_2157870
                          *std_configure_args
    system "make", "install"

    inreplace [bin/"xml2-config", lib/"pkgconfig/libxml-2.0.pc"] do |s|
      s.gsub! prefix, opt_prefix
      s.gsub! icu4c.prefix.realpath, icu4c.opt_prefix, audit_result: false
    end

    # `icu4c` is keg-only on macOS and can be during migration on Linux,
    # so we need to tell `pkg-config` where to find its modules.
    icu_uc_pc = icu4c.opt_lib/"pkgconfig/icu-uc.pc"
    inreplace lib/"pkgconfig/libxml-2.0.pc",
              /^Requires\.private:(.*)\bicu-uc\b(.*)$/,
              "Requires.private:\\1#{icu_uc_pc}\\2"
  end

  test do
    (testpath/"test.c").write <<~C
      #include <libxml/tree.h>

      int main()
      {
        xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
        xmlNodePtr root_node = xmlNewNode(NULL, BAD_CAST "root");
        xmlDocSetRootElement(doc, root_node);
        xmlFreeDoc(doc);
        return 0;
      }
    C

    # Test build with xml2-config
    args = shell_output("#{bin}/xml2-config --cflags --libs").split
    system ENV.cc, "test.c", "-o", "test", *args
    system "./test"

    # Test build with pkg-config
    ENV.append "PKG_CONFIG_PATH", lib/"pkgconfig"
    args = shell_output("#{Formula["pkgconf"].opt_bin}/pkgconf --cflags --libs libxml-2.0").split
    system ENV.cc, "test.c", "-o", "test", *args
    system "./test"

    # Make sure cellar paths are not baked into these files.
    [bin/"xml2-config", lib/"pkgconfig/libxml-2.0.pc"].each do |file|
      refute_match HOMEBREW_CELLAR.to_s, file.read
    end
  end
end
